

require('./common');

exports.load = function(req, res){
	var id = req.params.id;
	var tblname = req.params.tblname;
	var formName = req.params.formName;
	var newDate = new Date();
	var rCount=0;
	var users=["Stephen Cury","Harrison Barnes", "Kent Bazemore", "Andris Biedrins", "Andrew Bogut", "Festus Ezeli", "Draymond Green"];
	
	var strSQL = "SELECT * FROM " + tblname + ' ORDER BY ID ASC LIMIT 10 ';
	
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		var output = '<html><head></head><body><h4>Home / Forms /</h4><h2>' + formName +'</h2><table cellpadding=0 cellspacing=0 width="98%">';
		output += '<tr style="background-color:#f4f4f4">';
		output += '<td colspan=2><a href="form/' + id+ '/0/" class="classname">New Request</a></td>';
		output += '<td align="right"><input type="text" value="Search..." style="width:200px;font-style:Italic;color:#777" /></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3><hr></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3>';
		output += '<ul id="tabs">';
		output += '<li><a href="#" name="tab1"><img src="/images/record.png" style="vertical-align:middle" style="border:0px">&nbsp;&nbsp;My Requests</a></li>';
		output += '<li><a href="#" name="tab2"><img src="/images/record.png" style="vertical-align:middle" style="border:0">&nbsp;&nbsp;For My Approval</a></li>';
		output += '<li><a href="#" name="tab3"><img src="/images/record.png" style="vertical-align:middle" style="border:0">&nbsp;&nbsp;All</a></li>';
		output += '<li><a href="#" name="tab4"><img src="/images/record.png" style="vertical-align:middle" style="border:0">&nbsp;&nbsp;Announcement</a></li>';
		output += '</ul>';
		output += '</td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3>';
		output += '<div id="content">';
		output += '<div id="tab1" class="divTab">';
		output += '<table width="100%" cellspacing=0 cellpadding=5>';
		output += '<tr style="background-color:#f2f0bf">';
		output += '<td style="border:1px Solid #808a10" valign="top">No record found.</td>';
		output += '</tr>';
		output += '</table>';
		output += '</div>';
		output += '<div id="tab2" class="divTab">';
		output += '<table width="100%" cellspacing=0 cellpadding=5>';
		output += '<tr style="background-color:#f2f0bf">';
		output += '<td style="border:1px Solid #808a10" valign="top">No record found.</td>';
		output += '</tr>';
		output += '</table>';
		output += '</div>';
		output += '<div id="tab3" class="divTab">';
		output += '<table width="100%" cellspacing=0 cellpadding=5>';
		for (var index in results) {
			
			
			output += '<tr style="height:80px">';
			output += '<td style="border-bottom:1px Solid #eee;width:75px"><img src="/images/' +  results[index].ID +'.jpg" class=""></td>';
			output += '<td style="border-bottom:1px Solid #eee"><h4 style="display: inline;">'+ users[index] +'<font color="#777" style="font-weight:normal"> submitted a request with Tracking Number: </font></h4>';
			
		//	for (var findex in fields) {
				var sqlResult = results[index];
				var fieldName = fields[1].name;
				var fieldValue = sqlResult[fieldName];
				
			//	output +=  fieldName + ' : ' + fieldValue + ',  ';
		//	}
			output += '<a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">' + results[index].TrackNo + '</a>';
			output += '<br><br><a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">View</a>&nbsp;&nbsp;<a href="" class="editDelete">Approve</a>&nbsp;&nbsp;<a href="" class="editDelete">Reject</a>&nbsp;&nbsp;<a href="" class="editDelete">Comment</a>';
			output += '</td>';
			output += '<td align="right" style="border-bottom:1px Solid #eee"><font color="#777">' + newDate.getMonth() + "/" + newDate.getDate() + "/" + newDate.getFullYear() + '</td>';
			output += '</tr>';
		
			rCount++;
		}
		
		
		
		if(rCount==0){
			output += '<tr style="background-color:#f2f0bf">';
			output +=  '<td colspan=3 style="border:1px Solid #808a10" valign="top">No record found.</td>';
			output +=  '</tr>';
		}
		output += '</table></div>';
		
		
		// Add Announcement
		output += '<div id="tab4" class="divTab">';
		output += '<img src="/images/avatar/avatar.png" width="60" height="55"/>';
		output += '<textarea class="input-comment announcement" placeholder="How are you?"></textarea><br/>';
		output += '<input type="button" class="classname btnAnnouncement" value="Announcement" style="margin-left:65px;"><br/>';
		output += '<div class="hr"></div>';
		output += '<div class="postHere" style="float: left;"></div>';
		
		
		output += '</div>';
		output += '</td>';
		output += '</tr>';
	
		output += '</body></html>';
		
		
		res.writeHead(200, {'Content-Type': 'text/html'});
		res.end(output);
	});
}